package com.byq.mm.gateway;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;

import com.byq.mm.gateway.adapter.HomeAdapter;
import com.tc.gateway.device.GeneralDevice;
import com.tc.gateway.device.Group;
import com.tc.gateway.event.GroupAddRespEvent;
import com.tc.gateway.event.GroupRemoveRespEvent;



public class GroupEditActivity extends Activity{
	AssistanceApplication application;
	Group g;
    GridView groupEditGrid;
    GridView otherGrid;
    BroadcastReceiver messageReceiver;
    void updateAll(){
    	updateGrid(groupEditGrid);
    	updateGrid(otherGrid);

    }
    void updateGrid(GridView v){
    	HomeAdapter ad = (HomeAdapter)v.getAdapter();
    	ad.reset();
    	v.setAdapter(ad);
    	
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   WindowManager.LayoutParams.FLAG_FULLSCREEN);  
        setContentView(R.layout.groupedit);
        
        
        groupEditGrid = (GridView) findViewById(R.id.groupEditGrid);
        otherGrid = (GridView) findViewById(R.id.otherGrid);
        Button b = (Button) findViewById(R.id.saveScene);
        b.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				storeScene();
				
			}
		});
         application = (AssistanceApplication)this.getApplication();
		
		Short gid = (Short)this.getIntent().getExtras().get("groupId");
		g = application.getHomeApplication().getGatewayEngine().getGroupManager().get(gid);
        
        	groupEditGrid.setAdapter(new HomeAdapter(application,GroupEditActivity.this){

				@Override
				protected boolean action(int position) {
					buffer = (GeneralDevice)this.getItem(position);
					showDialog(1);
					return false;
				}

				@Override
				protected void fetch() {
					devices = g.getMembers();
					
				}
				
			});

        	otherGrid.setAdapter(new HomeAdapter(application,GroupEditActivity.this){

				@Override
				protected boolean action(int position) {
					buffer = (GeneralDevice)this.getItem(position);
					showDialog(0);
					return false;
				}

				@Override
				protected void fetch() {
		        	List data = this.application.getHomeApplication().getGatewayEngine().getGroupManager().notmembers(g.getId());
		        	devices = data;
					
				}
				
			});
        	

        messageReceiver = new BroadcastReceiver(){

    		@Override
    		public void onReceive(Context context, Intent intent) {
    			if(intent.getAction().equals(GroupAddRespEvent.class.getName())
    					||intent.getAction().equals(GroupRemoveRespEvent.class.getName())){
    				updateAll();
    			}
    			
    				
    			if(intent.getAction().equals(AssistanceAction.ACTION_HOME_ERROR)){
    				Toast.makeText(GroupEditActivity.this, "", Toast.LENGTH_SHORT).show();
    				updateAll();
    				
    			}
    			
    		}
    		
    	};
       
                
                IntentFilter mf = new IntentFilter();
                mf.addAction(GroupRemoveRespEvent.class.getName());
                mf.addAction(GroupAddRespEvent.class.getName());
        		mf.addAction(AssistanceAction.ACTION_HOME_UPDATE);
        		mf.addAction(AssistanceAction.ACTION_HOME_ERROR);
        		registerReceiver(messageReceiver, mf);	
        		
        		
        		groupEditGrid.setOnTouchListener(new View.OnTouchListener() {
					
					@Override
					public boolean onTouch(View v, MotionEvent event) {
						
						return false;
					}
				});
        		
        		
    }
    GeneralDevice buffer;
    @Override
    protected Dialog onCreateDialog(int id) {
    		if(id == 0){
            return new AlertDialog.Builder(GroupEditActivity.this)

                .setTitle("��Ҫ����?")
                .setPositiveButton("ȷ��", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    	add();
                        
                    }
                })
                .setNegativeButton("ȡ��", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                       
                    }
                })
                .create();
    }else{
    	return new AlertDialog.Builder(GroupEditActivity.this)

        .setTitle("��Ҫ����?")
        .setPositiveButton("ȷ��", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {

                remove();
            }
        })
        .setNegativeButton("ȡ��", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {

               
            }
        })
        .create();
    }
        }
    @Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if(messageReceiver == null)
			return;
		this.unregisterReceiver(messageReceiver);
		this.messageReceiver = null;
	}
    void add() {
		GeneralDevice e = buffer;
		
		application.getHomeApplication().getGatewayEngine().getGroupManager().addDevice(e.getId(), g.getId(), null);
		
	}
    void remove() {

    	application.getHomeApplication().getGatewayEngine().getGroupManager().remove(buffer.getId(), g.getId());
		
	}
    private void storeScene() {
		for(GeneralDevice e: g.getMembers()){
			application.getHomeApplication().getGatewayEngine().getGroupManager().storeScene(e.getId(), g.getId());
		
		}
		
	}
}
